﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using ControleRepresentante.Logic;
using ControleRepresentante.Logic.Models;
using ControleRepresentante.Logic.Repositories;
using ControleRepresentante.Logic.Repositories.SearchExtensions;
using WebSite.ViewModels.ViewModelExtensions.DetailsViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.EditViewModelExtensions;
using WebSite.ViewModels.ViewModelExtensions.IndexViewModelExtensions;

namespace WebSite.Controllers.ControllerExtensions
{
    public class TransportadoraControllerJolitexExtension : ControllerJolitexExtensionBase<Transportadora>
    {
        protected override IIndexViewModelExtension DoGetIndexViewModelExtension(FormCollection formCollection, IRepository repository)
        {
            string codigo = formCollection["codigo"];
            return new TransportadoraJolitexIndexViewModel(codigo);
        }

        public override IEditViewModelExtension EditViewModelExtension
        {
            get { return new TransportadoraJolitexEditViewModel(); }
        }

        public override IDetailsViewModelExtension DetailsViewModelExtension
        {
            get { return new TransportadoraJolitexDetailsViewModel(); }
        }

        public override ISearchExtension GetSearchExtension(FormCollection formCollection)
        {
            return new TransportadoraJolitexSearchExtension(GetCodigo(formCollection));
        }

        public override void RegisterIndexColumns(IIndexColumnsProvider indexColumnsProvider)
        {
            indexColumnsProvider.RegisterColumn("Código Jolitex", TransportadoraJolitexIndexColumns.Codigo);
        }

        protected override Dictionary<string, string> TryUpdateModel(Transportadora model, FormCollection formCollection)
        {
            model.TransportadoraJolitex.Codigo = GetCodigo(formCollection);

            return new Dictionary<string, string>();
        }

        protected override Area Area { get { return Area.Transportadoras; } }

        protected override void SetModel(Transportadora model)
        {
            if (model.TransportadoraJolitex == null)
            {
                model.SetTransportadoraJolitex();
            }
        }

        private static int? GetCodigo(FormCollection formCollection)
        {
            string codigoString = formCollection["codigo"];
            int codigoInt;
            return Int32.TryParse(codigoString, out codigoInt) ? (int?) codigoInt : null;
        }
    }
}